--
-- Author: shunguo.chen
-- Date: 2018-06-27 14:32:51
--
-- WorldBlessController
--

local WorldBlessView = import("..views.WorldBlessView")
local BlessView = import("..views.BlessView")
local BlessRewardView = import("..views.BlessRewardView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("WorldMapModel")

end

function ClassRef:removeListeners()
    self.super.removeListeners(self)

end

function ClassRef:listEvent()
    return { "onDailyUpdate" }
end

function ClassRef:onEvent(eventName, data)
    if eventName == "onDailyUpdate" then
        -- 跨天，清除数据和界面，重新打开会更新数据
        self._model:clearBlessRewards()
        self:closeView()
        UIHandler:removePopupUiByKey(BlessView:getViewName())
        UIHandler:removePopupUiByKey(BlessRewardView:getViewName())
    end
end

function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._view = WorldBlessView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view, data)

    self._view:setVisible(false)
    self._model:reqWorldBlessInfo(function()
        if cc.isDead(self._view) then return end
        self._view:show()
        local mapInfos = self._model:getMapInfos()
        local blessTimes = self._model:getBlessTimes()
        self._view:refresh(mapInfos)
        self._view:refreshBlessTimes(blessTimes)
    end)

    local bc = app:getInst("BiddingController")
    bc:reqTownList()
end

-- mapInfo = MapInfo
function ClassRef:openBlessView(mapInfo)
    if not self:isViewAlive() then return end

    local view = BlessView.new(app.mvc.VIEWTYPE_MODAL_BG)
    view:onCloseView(function(sender)
        sender:removeFromParent()
    end)
    view:onOperate(function(op, data)
        if op == "openBlessReward" then
            self:openBlessRewardView(data)
        end
    end)

    local blessTimes = self._model:getBlessTimes()
    view:refreshBlessTimes(blessTimes)
    view:refresh(mapInfo)
    view:setName(view:getViewName())
    self._view:addChild(view)
    view:show()
end

-- zoneInfo = ZoneInfo
function ClassRef:openBlessRewardView(zoneInfo)
    if not self:isViewAlive() then return end

    local view = BlessRewardView.new(app.mvc.VIEWTYPE_MODAL_BG)
    view:onCloseView(function(sender)
        sender:removeFromParent()
    end)
    view:onOperate(function(op, data)
        if op == "reqBless" then
            self._model:reqBlessZone(data.id)
        end
    end)

    local blessTimes = self._model:getBlessTimes()
    view:refreshBlessTimes(blessTimes)
    local blessRewards = self._model:getBlessRewards(zoneInfo.id)
    view:refreshRewards(blessRewards)
    view:refresh(zoneInfo)
    view:setName(view:getViewName())
    self._view:addChild(view)
    view:show()
end

-------------------------------------------------------
function ClassRef:onBlessZone(zoneInfo, blessTimes)
    if not self:isViewAlive() then return end

    self._view:refreshBlessTimes(blessTimes)

    -- BlessView ui update
    local view = self._view:getChildByName(BlessView:getViewName())
    if view then
        view:refreshBlessTimes(blessTimes)
        view:updateZone(zoneInfo)
    end
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    -- print("WorldBlessController:onOperate op", op)
    if op == "openBless" then
        self:openBlessView(data)
    end
end

return ClassRef
